package com.app.couponsharelife.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.LifeShopPageBean
import com.app.couponsharelife.databinding.LocalShopDetailCouponListItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener
import java.math.BigDecimal

class LocalShopDetailCouponAdapter(val list: List<LifeShopPageBean.CardsBean>,
                                   private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.local_shop_detail_coupon_list_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }


    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        val cardsBean = list[position]
        (holder as ViewHolder).databinding.tvTitle.text = if (cardsBean.cardType == "CASH")
            "￥${cardsBean.reduceMoney}" else "折扣券"
        holder.databinding.tvContent.text = if (cardsBean.cardType == "CASH")
            "满${cardsBean.leastMoneyToUse}减${cardsBean.reduceMoney}" else
            "${BigDecimal(100).subtract(BigDecimal(cardsBean.discount))
                    .setScale(1, BigDecimal.ROUND_HALF_UP)}折优惠券"
        if (cardsBean.isAllowToReceive!!) {
            holder.databinding.tvCouponStatus.text = "已 领"
            //holder.databinding.tvCouponStatus.setBackgroundResource(R.drawable.act_local_shop_detail_coupon_item_get_bg)
        } else {
            holder.databinding.tvCouponStatus.text = "领 取"
            //holder.databinding.tvCouponStatus.setBackgroundResource(R.drawable.act_local_shop_detail_coupon_item_no_get_bg)
        }
    }

    inner class ViewHolder(val databinding: LocalShopDetailCouponListItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener { listener.click(0, "") }
        }
    }
}